package com.dublbo.addressbook.frame.group;

import com.dublbo.addressbook.frame.CPersonMainPanel;
import com.dublbo.addressbook.util.FileOperation;
import com.dublbo.jpSwing.JpToaster;
import com.dublbo.jpSwing.thread.JpShortTask;

public class GroupDeleteTask extends JpShortTask {

    private final CPersonMainPanel ui;

    public GroupDeleteTask(CPersonMainPanel ui) {
        this.ui = ui;
    }

    @Override
    protected void doInBackground() throws Exception {
        String groupName = (String) this.args[0];
        String[] delPersonArr = (String[]) this.args[1];

        FileOperation.deleteGroupTxtFileRow(groupName);
        System.out.println("已成功删除联系组：" + groupName);

        // 删除该组联系人
        if (delPersonArr != null) {
            for (String s : delPersonArr) {
                FileOperation.deleteCPersonTxtFileRow(s);
            }
            System.out.println("已成功删除该组的 " + delPersonArr.length + " 位联系人");
        }
    }

    @Override
    protected void done() {
        if (this.err != null) {
            JpToaster.show(this.ui, JpToaster.ERROR, this.err.getMessage());
            return;
        }
        // 更新左侧的菜单栏
        int index = this.ui.left_menu.getSelectedIndex();
        this.ui.left_menu.setSelectedIndex(1);
        this.ui.left_menu.removeTabAt(index);
    }
}
